# Copyright (c) 2005-2010, Paolo Amoroso (Aioe) <freedom@aioe.org>
# All rights reserved.
# Postfilter (access.pm) version 0.8.2

use strict;

use Net::DNS;
use Date::Parse;
use Digest;
use Digest::MD5;
use Digest::SHA1;


our (%hdr, $modify_headers, $body, $user, %config, %settings, %postersdefault );
our (%headlist, @quickref, @saved_headers, %whitelist, @distributions, %forbidden_crosspost, %scoreset, %maxscore);
our (@nomoderation, @htmlallowed, @htmltags, %extracontent, %forbidden_headers, @forbidden_groups, %dnsbl, @localip );


########
#
# check_user(): Check whether the sender has the right to post
#
#######


sub check_user()
{
	my ($host, $time, $pid, $ip, $date, $client_dn, $body_length, $head_length, $md5, $domain, $gruppi, $followup) = &query_cache_header();

	&log("notice", "Checking user rights for $ip ($client_dn), user $user" );

	return 0;
}

sub write_access($)
{
	my $error_code = $_[0];
	my ($host, $time, $pid, $ip, $date, $client_dn, $body_length, $head_length, $md5, $domain, $gruppi, $followup) = &query_cache_header();


	my $ACCESS;
	unless (open( $ACCESS, "$config{'file_access'}"))
	{
                &log( "err", "Unable to read data from $config{'file_access'}" );
                return (1);
        }

	my ($line,$expired_messages,$total_messages,@goodlines,$entries);

# Read one line per time and expire

	my $last_expired = $time - $config{'trash_period'};
	$expired_messages = 0;


	while ( $line = <$ACCESS> )
	{	
	        my @data = split( /\t/, $line );
                if ( $data[0] < $last_expired )
                {
                        $line = "";
                        $expired_messages++;
                }
                $total_messages++;
		$entries = push(@goodlines, $line ) if ( $line ne "" );
	}

	close $ACCESS;

	my $goodlines = $total_messages - $expired_messages;
	&log("notice", "Spool File: Time $time, Expire $last_expired, Total $total_messages/$entries, expired $expired_messages, still good $goodlines" );

	unless (open( $ACCESS, ">$config{'file_access'}"))
        {
                &log( "err", "Unable to read data from $config{'file_access'}" );
                return (1);
        }

	foreach( @goodlines )
	{
		print $ACCESS $_;
	}

	my $total_length = $body_length + $head_length;
	my $msgid = $hdr{'Message-ID'};

	print $ACCESS "$time\t$error_code\t$msgid\t$ip\t$client_dn\t$domain\t$user\t$total_length\t$head_length\t$body_length\t$gruppi\t$followup\t$md5\n";
	close $ACCESS;
	
	

	return 0;
}


1;
